/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.engin.moves;

import ac.settings.dataModel;
import static ac.settings.dataModel.TABLE_clients_data;
import static ac.settings.dataModel.TABLE_products;
import ac.settings.files;
import static ac.settings.files.REPORT_sales_status;
import ac.settings.sqlQuerys;
import ac.ui.FrameOperationTemplat;
import ac.ui.utils;
import ac.uitls.propertiesOperation;
import java.awt.Component;
import java.util.Date;

/**
 *
 * @author empcl_000
 */
public class pricView extends FrameOperationTemplat implements dataModel, sqlQuerys, files {

    propertiesOperation po = new propertiesOperation();

    private String meidField = "pricview_id";
    ///editableForCopy
    private String Tabel = TABLE_PRICEVIEW;
    ///editableForCopy
    private String Items_Tables = TABLE_sales_items;
    ///editableForCopy
    private String order_Tables = TABLE_inventory_order_output;
    ///editableForCopy
    private String BillTable = TABLE_sales;
    ///editableForCopy
    private String orderField = "inventory_order_id";
    private String sQLALL = SQL_BAYMoveAll;
    private String ReportStatus = REPORT_bay_status;

    ///
    private String BillmeidField = "bill_id";
    ac.engin.index ix = new ac.engin.index();
    ac.engin.archive.clients c = new ac.engin.archive.clients();

    //<editor-fold defaultstate="collapsed" desc="process">
    ///editableForCopy
    public int reProcess(String Cid, double am) {
        return 0;
    }
///editableForCopy

    public int doProcess(String Cid, double am) {
        return 0;
    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="status">
    public String getStatus(String id) {
        String[] m = getMove(id);
        return m[6];
    }

    public int setStatus(String id, String status) {

        String[][] usedata = {new String[]{"status"}, new String[]{status}};

        db.setUpdate(Tabel, usedata, "id='" + id + "'");
        return db.retuenstatus;

    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="get Date">
    public String getIdByCode(String code) {
        String[] r;
        r = db.getRow(Tabel, "*", "code='" + code + "'");
        return r[5];

    }

    public String[] getMoveItems(String id) {
        String[] r;
        r = db.getRow(Items_Tables, "*", "id='" + id + "'");
        return r;

    }

    public String[] getMove(String id) {
        String[] r;
        r = db.getRow(Tabel, "*", "id='" + id + "'");
        return r;

    }

    public String[] returnLastSellMove() {
        String[] r;
        r = db.getRow(Tabel, "*", "id='" + returnLastID() + "' and rowstatus ='" + rowNormal + "'");
        return r;

    }

    public String returnLastID() {
        String[] dataMax = db.getRow(Tabel, "max(id) as m", "rowstatus ='" + rowNormal + "'");
        return dataMax[0];

    }

//</editor-fold>
    ///editableForCopy
    //<editor-fold defaultstate="collapsed" desc="inventory">
    public String getOrderID(String Bid) {
        String R = "0";

        String[] ishvaeone = db.getRow(order_Tables, "*", meidField + "='" + Bid + "'");
        if (ishvaeone[0] != null) {
            R = ishvaeone[0];
        }
        return R;
    }

    public int creatInventoryOrder(String Bid, Component magParent) {
        int r = 0;
        String[] ishvaeone = db.getRow(order_Tables, "*", meidField + "='" + Bid + "'");

        if (ishvaeone[0] != null) {
            utils.sendCreateOrderError(magParent, ishvaeone[0]);
        } else {

            String[] Bdata = getMove(Bid);
            String c = IreturnLastID();
            String[][] data
                    = new String[][]{{"code", "client_title", "client", "date", meidField}, {c, Bdata[8], Bdata[1], Bdata[3], Bid}};

            db.setInsert(order_Tables, data);
            SellitemsToinventory(Bid);
            c = IreturnLastID();
            updateOrderId(Bid, c);
            ///editableForCopy
            ix.inventoryOuput("update", c);

        }

        return r;

    }

    public void updateOrderId(String bid, String orderid) {

        String[][] data = {new String[]{orderField}, new String[]{orderid}};
        db.setUpdate(Tabel, data, "id='" + bid + "'");
    }

    public String IreturnLastID() {
        String[] dataMax = db.getRow(order_Tables, "max(id) as m", "rowstatus ='" + rowNormal + "'");
        String r = "1";

        if (dataMax[0] != null) {

            r = dataMax[0];

        }
        return r;

    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="treasury">
    public void treasuryin(String Bid) {
        String[] sellData = getMove(Bid);
        String[] d = new String[]{"noValue", "noValue", sellData[1], "noValue", "noValue", getPricBill(Bid), po.getWord("label.sellMove") + " " + po.getWord("label.number") + sellData[0], sellData[4], "noValue"};
        ///editableForCopy
        ix.treasuryInput("notsaved", d);

    }
//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Bill">
    public String getmyBillID(String Bid) {
        String R = "0";

        String[] ishvaeone = db.getRow(BillTable, "*", meidField + "='" + Bid + "'");
        if (ishvaeone[0] != null) {
            R = ishvaeone[0];
        }
        return R;
    }

    public int creatBill(String Bid, Component magParent) {
        int r = 0;

        //
        String[] ishvaeone = db.getRow(BillTable, "*", meidField + "='" + Bid + "'");

        if (ishvaeone[0] != null) {
            utils.sendCreateBillError(magParent, ishvaeone[0]);
        } else {
            String[] Bdata = getMove(Bid);

            String c = BillreturnLastID();

            String[][] data
                    = new String[][]{{"code", "client_title", "client", "date", meidField}, {c, Bdata[8], Bdata[1], Bdata[3], Bid}};

            db.setInsert(BillTable, data);

            itemsFrominventoryTobill(Bid);
            c = BillreturnLastID();
            updateBillId(Bid, c);
///editabel
            ac.engin.index ix = new ac.engin.index();
            ix.sellMoveOpen("update", c);
        }
        return r;

    }

    public void itemsFrominventoryTobill(String id) {
        String n = BillreturnLastID();

        String[][] data = {new String[]{BillmeidField}, new String[]{n}};

        db.setUpdate(Items_Tables, data, meidField + "='" + id + "'");
    }

    public void updateBillId(String bid, String orderid) {

        String[][] data = {new String[]{BillmeidField}, new String[]{orderid}};
        db.setUpdate(Tabel, data, "id='" + bid + "'");
    }

    public String BillreturnLastID() {
        String[] dataMax = db.getRow(BillTable, "max(id) as m", "");
        String r = "1";

        if (dataMax[0] != null) {

            r = dataMax[0];

        }
        return r;

    }

//</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Items">
    public String getBillID(String id) {

        return db.getRow(Items_Tables, meidField, "`id`='" + id + "'")[0];

    }

    public String getPris(String id) {

        return db.getRow(Items_Tables, "price", "`id`='" + id + "' and  rowstatus ='" + rowNormal + "'")[0];

    }

    public String getPricBill(String id) {

        String d = db.getRow(Items_Tables, "sum(price) as m", "`" + meidField + "`='" + id + "' and  rowstatus ='" + rowNormal + "'")[0];

        if (d == null) {
            d = "0";
        }

        return d;

    }

    public void SellitemsToinventory(String id) {
        String n = IreturnLastID();

        String[][] data = {new String[]{orderField}, new String[]{n}};

        db.setUpdate(Items_Tables, data, meidField + "='" + id + "'");
    }

    public String deletItems(String id) {

        String[][] data = {new String[]{meidField, "del_bill_id"}, new String[]{"-1", id}};

        return db.setUpdate(Items_Tables, data, meidField + "='" + id + "'");

    }

    public String resItems(String id) {

        String[][] data = {new String[]{meidField, "del_bill_id"}, new String[]{id, "0"}};

        return db.setUpdate(Items_Tables, data, "del_bill_id='" + id + "' and " + meidField + "='-1'");

    }

    public String returnLastItemID() {
        String[] dataMax = db.getRow(Items_Tables, "max(id) as m", "  rowstatus ='" + rowNormal + "'");
        return dataMax[0];

    }

//</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="reportes">
    
    public void reportView(String url, String where, String[] updateTitles) {
        TabelString = Tabel;
        ac.reportes.reportes.updateReporteTitles(updateTitles[0],
                updateTitles[1],
                updateTitles[2],
                updateTitles[3],
                updateTitles[4]);
        
        setPrint(url, sQLALL + where);
    }
    
    public void reportViewForPorduect(String Porduectid, Date d1, Date d2) {
        String[] reptitles = new String[5];
        
        String where = " where 1=1 ";
        
        if (Porduectid != null && !Porduectid.equals("")) {
            String title = db.getRow(TABLE_products, "*", Integer.parseInt(Porduectid))[1];
            where = " and   " + Items_Tables + ".product='" + Porduectid + "' ";
            reptitles[1] = po.getWord("label.product_title") + " : " + title;
            
        } else {
            
            reptitles[1] = "-";
            
        }
        
        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_salesForProduct");
        reptitles[3] = "-";
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }
    
    public void reportViewForPorduectClinte(String Porduectid, String Clinte_id, Date d1, Date d2) {
        String[] reptitles = new String[5];
        
        String where = " where 1=1 ";
        
        if (Porduectid != null && !Porduectid.equals("")) {
            String title = db.getRow(TABLE_products, "*", Integer.parseInt(Porduectid))[1];
            where = " and   " + Items_Tables + ".product='" + Porduectid + "' ";
            reptitles[3] = po.getWord("label.product_title") + " : " + title;
            
        } else {
            
            reptitles[3] = "-";
            
        }
        
        if (Clinte_id != null && !Clinte_id.equals("")) {
            String clinte = db.getRow(TABLE_clients_data, "*", Integer.parseInt(Clinte_id))[1];
            reptitles[1] = po.getWord("label.client_title") + " : " + clinte;
            
            where += " and " + Tabel + ".client='" + Clinte_id + "'";
            
        } else {
            
            reptitles[1] = "-";
            
        }
        
        if (d1 != null && d2 != null) {
            where += " and   " + ac.lib.data.dateTime.updateSerechDates(Tabel + ".date", d1, d2);
            reptitles[2] = po.getWord("label.from") + " " + d1.toString() + " " + po.getWord("label.to") + " " + d2.toString();
        } else {
            reptitles[2] = "-";
        }
        reptitles[0] = po.getWord("title.report_salesForProduct");
        
        reptitles[4] = "-";
        this.reportView(ReportStatus, where, reptitles);
    }
//</editor-fold>

    public void Installment(String id) {

        ix.InstallmentOpen("sell", id);

    }
}
